<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺id" prop="merId">
        <el-input
          v-model="queryParams.merId"
          placeholder="请输入店铺id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单详情id" prop="orderDetailId">
        <el-input
          v-model="queryParams.orderDetailId"
          placeholder="请输入订单详情id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品id" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入商品id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品规格属性id" prop="attrValueId">
        <el-input
          v-model="queryParams.attrValueId"
          placeholder="请输入商品规格属性id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品规格属性值,多个,号隔开" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入商品规格属性值,多个,号隔开"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="星级" prop="star">
        <el-input
          v-model="queryParams.star"
          placeholder="请输入星级"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管理员回复时间" prop="merchantReplyTime">
        <el-date-picker clearable
          v-model="queryParams.merchantReplyTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择管理员回复时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="0未回复1已回复" prop="isReply">
        <el-input
          v-model="queryParams.isReply"
          placeholder="请输入0未回复1已回复"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户头像" prop="avatar">
        <el-input
          v-model="queryParams.avatar"
          placeholder="请输入用户头像"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:reply:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:reply:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:reply:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:reply:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="replyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评论ID" align="center" prop="id" />
      <el-table-column label="用户ID" align="center" prop="uid" />
      <el-table-column label="店铺id" align="center" prop="merId" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="订单详情id" align="center" prop="orderDetailId" />
      <el-table-column label="商品id" align="center" prop="productId" />
      <el-table-column label="商品规格属性id" align="center" prop="attrValueId" />
      <el-table-column label="商品规格属性值,多个,号隔开" align="center" prop="sku" />
      <el-table-column label="星级" align="center" prop="star" />
      <el-table-column label="评论内容" align="center" prop="comment" />
      <el-table-column label="评论图片" align="center" prop="pics" />
      <el-table-column label="管理员回复内容" align="center" prop="merchantReplyContent" />
      <el-table-column label="管理员回复时间" align="center" prop="merchantReplyTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.merchantReplyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="0未删除1已删除" align="center" prop="isDel" />
      <el-table-column label="0未回复1已回复" align="center" prop="isReply" />
      <el-table-column label="用户名称" align="center" prop="nickname" />
      <el-table-column label="用户头像" align="center" prop="avatar" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:reply:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:reply:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评论对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="replyRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="店铺id" prop="merId">
          <el-input v-model="form.merId" placeholder="请输入店铺id" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="订单详情id" prop="orderDetailId">
          <el-input v-model="form.orderDetailId" placeholder="请输入订单详情id" />
        </el-form-item>
        <el-form-item label="商品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="商品规格属性id" prop="attrValueId">
          <el-input v-model="form.attrValueId" placeholder="请输入商品规格属性id" />
        </el-form-item>
        <el-form-item label="商品规格属性值,多个,号隔开" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入商品规格属性值,多个,号隔开" />
        </el-form-item>
        <el-form-item label="星级" prop="star">
          <el-input v-model="form.star" placeholder="请输入星级" />
        </el-form-item>
        <el-form-item label="评论内容" prop="comment">
          <el-input v-model="form.comment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="评论图片" prop="pics">
          <el-input v-model="form.pics" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="管理员回复内容">
          <editor v-model="form.merchantReplyContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="管理员回复时间" prop="merchantReplyTime">
          <el-date-picker clearable
            v-model="form.merchantReplyTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择管理员回复时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="0未回复1已回复" prop="isReply">
          <el-input v-model="form.isReply" placeholder="请输入0未回复1已回复" />
        </el-form-item>
        <el-form-item label="用户名称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="用户头像" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入用户头像" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Reply">
import { listReply, getReply, delReply, addReply, updateReply } from "@/api/system/reply";

const { proxy } = getCurrentInstance();

const replyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    uid: null,
    merId: null,
    orderNo: null,
    orderDetailId: null,
    productId: null,
    attrValueId: null,
    sku: null,
    star: null,
    comment: null,
    pics: null,
    merchantReplyContent: null,
    merchantReplyTime: null,
    isDel: null,
    isReply: null,
    nickname: null,
    avatar: null,
  },
  rules: {
    uid: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    merId: [
      { required: true, message: "店铺id不能为空", trigger: "blur" }
    ],
    orderNo: [
      { required: true, message: "订单编号不能为空", trigger: "blur" }
    ],
    orderDetailId: [
      { required: true, message: "订单详情id不能为空", trigger: "blur" }
    ],
    productId: [
      { required: true, message: "商品id不能为空", trigger: "blur" }
    ],
    attrValueId: [
      { required: true, message: "商品规格属性id不能为空", trigger: "blur" }
    ],
    sku: [
      { required: true, message: "商品规格属性值,多个,号隔开不能为空", trigger: "blur" }
    ],
    star: [
      { required: true, message: "星级不能为空", trigger: "blur" }
    ],
    comment: [
      { required: true, message: "评论内容不能为空", trigger: "blur" }
    ],
    isDel: [
      { required: true, message: "0未删除1已删除不能为空", trigger: "blur" }
    ],
    isReply: [
      { required: true, message: "0未回复1已回复不能为空", trigger: "blur" }
    ],
    nickname: [
      { required: true, message: "用户名称不能为空", trigger: "blur" }
    ],
    avatar: [
      { required: true, message: "用户头像不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询评论列表 */
function getList() {
  loading.value = true;
  listReply(queryParams.value).then(response => {
    replyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    uid: null,
    merId: null,
    orderNo: null,
    orderDetailId: null,
    productId: null,
    attrValueId: null,
    sku: null,
    star: null,
    comment: null,
    pics: null,
    merchantReplyContent: null,
    merchantReplyTime: null,
    isDel: null,
    isReply: null,
    nickname: null,
    avatar: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("replyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加评论";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getReply(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改评论";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["replyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateReply(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addReply(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除评论编号为"' + _ids + '"的数据项？').then(function() {
    return delReply(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/reply/export', {
    ...queryParams.value
  }, `reply_${new Date().getTime()}.xlsx`)
}

getList();
</script>
